package com.ymq.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ymq.reggie.common.R;
import com.ymq.reggie.entity.User;
import com.ymq.reggie.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.ObjectUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Map;
import java.util.Objects;
import java.util.concurrent.TimeUnit;

@Slf4j
@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;
    @Autowired
    private RedisTemplate redisTemplate;

    @PostMapping("/login")
    public R<User> login(@RequestBody Map map, HttpSession session){
        System.out.println(map);
        log.info("map{}",map.toString());
        //手机号
        String phone = map.get("phone").toString();
        //验证码
        String keyMsg = map.get("code").toString();
       // = (String) session.getAttribute(phone);

        //从redis中拿到验证码信息
       // Object  attribute  = redisTemplate.opsForValue().get(phone);
        Object o = redisTemplate.opsForValue().get(phone);
        log.info("redis中code取值信息：{}",o);
        if(phone!=null&& ObjectUtils.equals(o,keyMsg)){
            //手机号是否注册
            LambdaQueryWrapper<User> lambdaQueryWrapper = new LambdaQueryWrapper<>();
               lambdaQueryWrapper.eq(User::getPhone,phone);
            User user = userService.getOne(lambdaQueryWrapper);
            if(user==null){
                 user = new User();
                user.setPhone(phone);
                user.setStatus(1);
                user.setName(phone.substring(0,5));
                userService.save(user);

            }
            session.setAttribute("user",user.getId());

            //登录成功后删除验证码信息
            redisTemplate.delete(phone);

           return  R.success(user);

        }

        return R.error("登录失败");
    }

    /**
     * 发送验证码
     * @param user
     * @return
     */
    @PostMapping("/sendMsg")
    public R<String> sendMsg(@RequestBody User user, HttpSession httpSession){
        String phone = user.getPhone();
        String subect="瑞吉外卖";
        if (StringUtils.isNotEmpty(phone)){
            String  code="3333";
            //得到一个长度为4的随机数
           // String code = ValidateCodeUtils.generateValidateCode(4);
            //阿里云短信服务发送验证码
          //  SMSUtils.sendMessage("令牌名"," ",phone,code);
            //qq邮箱
            String text= "【"+subect+"】"+ "你好，您的验证码为："+code+"请尽快登录，如非本人操作，请忽略此邮件。";
            userService.sendMsg(phone,subect,text);
            System.out.println("subect = " + subect);
            System.out.println("phone = " + phone);
            redisTemplate.opsForValue().set(phone,code,5,TimeUnit.MINUTES); //五分钟有效期

            return R.success("验证码已发送");
        }


        return  R.error("手机号为空");
    }

    /**
     * 退出登录
     * @param httpServletRequest
     * @return
     */
    @PostMapping("/loginout")
    public R<String> loginout(HttpServletRequest httpServletRequest){
        httpServletRequest.getSession().removeAttribute("user");
        return R.success("退出成功");
    }


}
